package com.wl.day45.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wl.day45.biz.UsersBiz;
import com.wl.day45.model.Users;
import com.wl.day45.util.BusinessException;
import com.wl.day45.util.Constants;

@Controller
@RequestMapping("/users")
public class UsersController {
	@Resource
	private UsersBiz usersBiz;
	
	@RequestMapping("/login/page")
	public String toLogin(){
		return "/login";
	}
	
	@RequestMapping("/login")
	public String login(Users user, HttpSession session)throws BusinessException{
		try {
			Users users = usersBiz.login(user);
			if(users == null){
				throw new BusinessException(Constants.LOGIN_EXCEPTION_CODE);
			}else{
				session.setAttribute("currentUser", users);
				return "/index";
			}
		} catch (BusinessException e) {
			e.printStackTrace();
			throw e;
		}
	}
	
}
